package com.wsc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wsc.entity.ConsumeLog;
import com.wsc.service.ConsumeLogService;

import net.sf.json.JSONObject;

@RequestMapping("/consumelog")
@RestController
@CrossOrigin
public class ConsumeLogController {

	@Autowired
	private ConsumeLogService cs;

	// 查 多条件 分页
	@RequestMapping("getAll")
	public Map<String, Object> getAll(ConsumeLog consumelog, Integer page, Integer limit) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("consumelog", consumelog);
		limit = limit == null ? 10 : limit;
		page = page == null ? 0 : page - 1;
		params.put("limit", limit);
		params.put("page", page);
		return cs.getAll(params);
	}

	/***
	 * 多条件 获取图表所需数据
	 * 
	 * @DateTimeFormat(pattern="yyyy-MM-dd")前台到后台时间格式转换
	 */
	@RequestMapping("getChartData")
	public Map<String, Object> getChartData(String intime, String outtime, Integer status,String reader) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> names = new ArrayList<String>();
		List<Integer> nums = new ArrayList<Integer>();
		// 饼图专用
		List<JSONObject> objects = new ArrayList<JSONObject>();
		// 原生sql获取数据 intime,outtime,status,reader
		List<Object[]> list = cs.getChartData(intime, outtime, status,reader);
		// 添加map集合
		for (int i = 0; i < list.size(); i++) {
			String name = (String) list.get(i)[0];
			// 原生sql count(*)查询结果类型为BigInteger
			Integer value = Integer.parseInt(list.get(i)[1].toString());
			names.add(name);
			nums.add(value);
			// 新建json对象 添加对象属性
			JSONObject object = new JSONObject();
			object.put("name", name);
			object.put("value", value);
			// 添加到数组
			objects.add(object);
		}
		map.put("names", names);
		map.put("nums", nums);
		map.put("objects", objects);
		return map;
	}
}
